<?php
namespace app\fly_pig\command;

// 导入类库
use app\fly_pig\controller\FlyPigOrderDetails;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
require_once EXTEND_PATH.'workerman'.DS.'Autoloader.php';

class FlyPigOrderDetail extends Command
{
    protected $worker; // worker实例
    protected $listen = 'http://0.0.0.0:2870'; // 监听地址

    /**
     * 显示命令
     *
     */
    protected function configure()
    {
        $this->setName('worker:fly_pig_order_detail')->setDescription('This is based on thinkphp5.0 to add the dispatch order end time command');
        $this->addArgument('action',Argument::OPTIONAL,"start|stop|restart|reload|status|connections",'start'); // Argument::OPTIONAL是可选的
        $this->addOption('host','H',Option::VALUE_OPTIONAL,'The host of the workerman server',null); // 主机地址
        $this->addOption('port','P',Option::VALUE_OPTIONAL,'The port of the workerman server',null); // 端口
        $this->addOption('daemon','d',Option::VALUE_NONE,'Workerman runs as a daemon.');
    }

    /**
     *  执行业务
     * @param Input $input
     * @param Output $output
     * @return int|void|null
     */
    protected function execute(Input $input, Output $output)
    {
        $this->worker = new \workerman\Worker($this->listen);
        $this->worker->count = 2; // 设置进程，设置多少个进程，就会启动多少个进程对外服务
        $this->worker->name = 'fly_pig_order_detail'; // 设置进程的名称
        $logPath = ROOT_PATH.'log';
        if (!file_exists($logPath)){
            mkdir($logPath,0777,true);
        }
        // 创建文件
        $file = fopen($logPath.DS.'fly_pig_order_detail.txt','w');
        if(!file_exists($logPath.DS.'fly_pig_order_detail.txt')){
            return false;
        }

        \workerman\Worker::$stdoutFile = $logPath.DS.'fly_pig_order_detail.txt'; // 以守护进程模式运行，设置输出文件

        $action = $input->getArgument('action');
        $action = empty($action) ? 'start':$action;
        $daemon = $input->getOption('daemon'); // 获取参数，是否是以守护进程的方式运行
        $os = 'WIN';
        // 判断当前的系统是linux，还是windows
        if (strtoupper(substr(PHP_OS,0,3)) === 'WIN'){
            $output->writeln("This is the windows system!\n");
            if ('start' != $action){
                $output->writeln("windows does not support this action:{$action}\n");
                return false;
            }
        } else {
            $output->writeln("This is the linux system!\n");
            $actionArray = ['start','stop','restart','reload','status','connections'];
            if (!in_array($action,$actionArray)){
                $output->writeln("Illegal parameter action:{$action},Legal parameter action:[start|stop|restart|reload|status|connections]\n");
                return false;
            }
            global $argv; // 重写全局变量argv
            $argv = [];
            array_push($argv,'FlyPigOrderDetail.php',$action);
            $os = 'linux';
        }
        switch ($action){
            case 'start':
                if (empty($daemon)){
                    $output->writeln("Start the workerman server in debug mode......\n");
                }elseif($daemon == '-d' && $os == 'linux'){ // 以守护进程模式运行，仅linux支持
                    \workerman\Worker::$daemonize = true;
                    array_push($argv,'-d');
                    $output->writeln("Start the workerman server as a daemon......\n");
                }else{
                    $output->writeln("Start the workerman server in debug mode......\n");
                }
                break;
            case 'stop':
                $output->writeln("Stop the workerman server......\n"); break;
            case 'restart':
                $output->writeln("Restart the workerman server......\n"); break;
            case 'reload':
                $output->writeln("Smoothly restart the workerman server......\n"); break;
            case 'status':
                $output->writeln("View the workerman server status......\n"); break;
            default:
                $output->writeln("Start the workerman server......\n");
        }
        // 创建订单类对象
        $hornet = new FlyPigOrderDetails();

        // 调用类的方法
        $this->worker->onWorkerStart = [$hornet,'onWorkerStart'];
        $this->worker->onConnect = [$hornet, 'onConnect']; // 连接时
        $this->worker->onMessage = [$hornet, 'onMessage']; // 连接时
        $this->worker->onClose = [$hornet, 'onClose']; // 连接时
        $this->worker->onWorkerStop = [$hornet, 'onWorkerStop']; // 连接时

        // 运行worker
        \workerman\Worker::runAll();
    }

}